#TODO 导入相关的python库
import os
from PIL import Image


def resize_images_in_folder(input_folder, output_folder, new_width, new_height):
    # 遍历输入文件夹中的所有文件
    for filename in os.listdir(input_folder):
        if filename.endswith(".jpg") or filename.endswith(".png"):
            # 构建输入和输出文件的路径
            input_image_path = os.path.join(input_folder, filename)
            output_image_path = os.path.join(output_folder, filename)

            # 调用resize_image函数修改图片分辨率
            resize_image(input_image_path, output_image_path, new_width, new_height)

def resize_image(input_image_path, output_image_path, new_width, new_height):
    image = Image.open(input_image_path)
    resized_image = image.resize((new_width, new_height))
    resized_image.save(output_image_path)

# 使用示例
input_folder = "E:\学校资料\赛题\样题新\样题\完整程序\任务一\data"
output_folder = "E:\学校资料\赛题\样题新\样题\完整程序\任务一\data_result"
new_width = 32
new_height = 32

resize_images_in_folder(input_folder, output_folder, new_width, new_height)

import os
import cv2


def resize_images_in_folder(input_folder, output_folder, new_width, new_height):
    # 如果输出文件夹不存在，则创建
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    # 遍历输入文件夹中的所有文件
    for filename in os.listdir(input_folder):
        if filename.endswith(".jpg") or filename.endswith(".png"):
            # 构建输入和输出文件的路径
            input_image_path = os.path.join(input_folder, filename)
            output_image_path = os.path.join(output_folder, filename)

            # 调用resize_image函数修改图片分辨率
            resize_image(input_image_path, output_image_path, new_width, new_height)


def resize_image(input_image_path, output_image_path, new_width, new_height):
    # 读取图像
    image = cv2.imread(input_image_path)
    # 调整图像尺寸
    resized_image = cv2.resize(image, (new_width, new_height))
    # 保存图像到输出路径
    cv2.imwrite(output_image_path, resized_image)


# 使用示例
input_folder = "num"
output_folder = "num_result"
new_width = 32
new_height = 32

resize_images_in_folder(input_folder, output_folder, new_width, new_height)
